﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Churchtoolkit.Web.Shell.Presenters;
using Churchtoolkit.Utils.ErrorProvider;
using System.Web.Security;
using System.Collections;
using Churchtoolkit.DataAccess.Entities;
using Churchtoolkit.Web.Shell.Views;
using Churchtoolkit.Utils.Navigation;


namespace Churchtoolkit.Web
{
    public partial class LoginView : Page<LoginPresenter> , ILoginView
    {
        #region [ Events ]
        /// <summary>
        /// Handles the Load event of the Page control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack)
            {
                this.vldreqTxtMail.Replace("[Field]","Email");
                this.vldreqTxtPass.Replace("[Field]", "Password");

                this.hplForgotYourPassword.NavigateUrl = Navigator.ResolveUrl(Pages.Shared_ForgetPassword);
                this.hplRegisterNow.NavigateUrl = Navigator.ResolveUrl(Pages.Shared_RegistrationView);
            }
        }

        /// <summary>
        /// Handles the InvalidMailOrPassword event of the txtPassword control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Web.UI.WebControls.ServerValidateEventArgs"/> instance containing the event data.</param>
        public void txtPassword_InvalidMailOrPassword(object sender, ServerValidateEventArgs e)
        {
            e.IsValid = this.Presenter.MemberExists(txtMail.Text, txtPass.Text);
        }

        /// <summary>
        /// Handles the NotActiveMember event of the txtPassword control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Web.UI.WebControls.ServerValidateEventArgs"/> instance containing the event data.</param>
        public void txtPassword_NotActiveMember(object sender, ServerValidateEventArgs e)
        {
            e.IsValid = this.Presenter.MemberIsNotActive();
        }

        /// <summary>
        /// Handles the Click event of the btnlogin control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void btnlogin_Click(object sender, EventArgs e)
        {
           if (IsValid)
            {
                FormsAuthentication.RedirectFromLoginPage(txtMail.Text, false);
            }
        }
        #endregion
    }
}